/**
 * Project Name:springmvc
 * File Name:NavigationController.java
 * Package Name:com.yichaoart.club.controller
 * Date:2016年5月28日下午8:49:46
 * Copyright (c) 2016,  All Rights Reserved.
 *
*/

package com.yichaoart.club.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.yichaoart.club.biz.TClubMemberServiceI;
import com.yichaoart.club.model.TClubMember;

import cn.springmvc.model.SysFunction;
import common.CommonStant;

/**
 * ClassName:NavigationController <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: 2016年5月28日 下午8:49:46 <br/>
 * 
 * @author i1093108915@yeah.net
 * @version
 * @since JDK 1.6
 * @see
 */
@Controller
@RequestMapping("/navigation")
public class NavigationController {

	@Autowired
	TClubMemberServiceI memberServiceI;

	@RequestMapping(value = "addMember")
	public ModelAndView addMember(HttpSession session, HttpServletRequest request, HttpServletResponse response,
			TClubMember member) {
		request.setAttribute("nav_active", "addMember");
		ModelAndView mv = new ModelAndView();
		if (!checkOnline(request)) {
			mv.setViewName("oprResult");
			return mv;
		}
		mv.setViewName("addMember");
		return mv;
	}

	@RequestMapping(value = "addOrder")
	public ModelAndView addOrder(HttpSession session, HttpServletRequest request, HttpServletResponse response,
			String sid) {
		ModelAndView mv = new ModelAndView();
		if (!checkOnline(request)) {
			mv.setViewName("oprResult");
			return mv;
		}
		request.setAttribute("nav_active", "addOrder");
		TClubMember member = memberServiceI.selectByPrimaryKey(sid);
		mv.addObject("member", member);
		mv.setViewName("addOrder");
		return mv;
	}

	/**
	 * checkOnline:(校验是否在线). <br/>
	 *
	 * @author i1093108915@yeah.net
	 * @param request
	 * @return
	 * @since JDK 1.6
	 */
	private boolean checkOnline(HttpServletRequest request) {
		if (request.getSession().getAttribute(CommonStant.LOGIN_ONLINE) == null) {
			return false;
		}
		return true;
	}

	/**
	 * login:(这里用一句话描述这个方法的作用). <br/>
	 *
	 * @author i1093108915@yeah.net
	 * @param request
	 * @param response
	 * @param email
	 * @param pass
	 * @return
	 * @since JDK 1.6
	 */
	@RequestMapping(value = "login")
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response, String email, String pass) {
		if (StringUtils.isNotEmpty(email) && StringUtils.isNotEmpty(pass)) {
			boolean auth = memberServiceI.checkAuth(email, pass);
			if (auth) {
				List<SysFunction> functions = memberServiceI.queryNavigationByUser(email);
				request.getSession().setAttribute(CommonStant.LOGIN_ONLINE, "true");
				TClubMember member  = memberServiceI.selectByEmail(email);
				request.getSession().setAttribute(CommonStant.LOGIN_USER, member);
				ModelAndView mv = new ModelAndView();
				mv.setViewName("index");
				mv.addObject("functions", functions);
				return mv;
			}
		}
		return null;
	}
}
